package com.zqwh.xtool.model.online;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zqwh.yj.sdk.utils.httpRequest.RetrofitSingleton;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.ProgressSubscriber;
import com.zqwh.yj.sdk.utils.httpRequest.HttpResultFunc2;
import com.zqwh.xtool.ToolsApp;
import com.zqwh.xtool.api.offline.LendOrderDetailResposity;
import com.zqwh.xtool.api.offline.LendOrderResposity;
import com.zqwh.xtool.api.offline.StockResposity;
import com.zqwh.xtool.api.online.SyncApi;
import com.zqwh.xtool.config.BaseConfig;
import com.zqwh.xtool.domain.SyncData;
import com.zqwh.xtool.domain.SyncDataATO;
import com.zqwh.xtool.greendao.db.LendOrder;
import com.zqwh.xtool.greendao.db.LendOrderDetail;
import com.zqwh.xtool.greendao.db.Stock;
import com.zqwh.xtool.greendao.db.User;
import com.zqwh.xtool.model.SyncModel;

import java.util.List;

import okhttp3.RequestBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SyncModelOnlineImpl implements SyncModel {
    private SyncApi syncApi;
    private StockResposity stockResposity;
    private LendOrderResposity lendOrderResposity;
    private LendOrderDetailResposity lendOrderDetailResposity;
    private Context context;

    public SyncModelOnlineImpl(Context context) {
        this.context = context;
        this.syncApi = RetrofitSingleton.getInstance(BaseConfig.BASE_URL).create(SyncApi.class);
        this.stockResposity = new StockResposity(this.context);
        this.lendOrderResposity = new LendOrderResposity(this.context);
        this.lendOrderDetailResposity = new LendOrderDetailResposity(this.context);
    }

    @Override
    public SyncDataATO getUpdatedData(){
        SyncDataATO syncDataATO = new SyncDataATO();
        syncDataATO.setDevId(ToolsApp.getDevId());
        syncDataATO.setStockATOs(stockResposity.queryStockListUpdated());
        syncDataATO.setLendOrderATOs(lendOrderResposity.queryLendOrderUpdated());
        syncDataATO.setLendOrderDetailATOs(lendOrderDetailResposity.queryLendOrderDetailUpdated());
        return syncDataATO;
    }

    @Override
    public boolean cancelUpdatedData(){
        boolean flag = true;
        List<Stock> stockList = stockResposity.queryStockListUpdated();
        for (Stock stock:stockList){
            stock.setUpdated(false);
        }
        List<LendOrder> lendOrderList = lendOrderResposity.queryLendOrderUpdated();
        for (LendOrder lendOrder:lendOrderList){
            lendOrder.setUpdated(false);
        }
        List<LendOrderDetail> lendOrderDetailList = lendOrderDetailResposity.queryLendOrderDetailUpdated();
        for (LendOrderDetail lendOrderDetail : lendOrderDetailList){
            lendOrderDetail.setUpdated(false);
        }
        if (!stockResposity.insertMultStock(stockList)){
            flag = false;
        }
        if (!lendOrderResposity.insertMultLendOrder(lendOrderList)){
            flag = false;
        }
        if (!lendOrderDetailResposity.insertMultLendOrderDetail(lendOrderDetailList)){
            flag = false;
        }
        return flag;
    }

    @Override
    public void cleanData(){
        stockResposity.deleteAll();
        lendOrderResposity.deleteAll();
        lendOrderDetailResposity.deleteAll();
    }

    @Override
    public void cleanStockData() {
        stockResposity.deleteAll();
    }

    @Override
    public void cleanLendOrderData() {
        lendOrderResposity.deleteAll();
    }

    @Override
    public void cleanLendOrderDetailData() {
        lendOrderDetailResposity.deleteAll();
    }

    @Override
    public void getSyncData(String sn, NetCallback<SyncData> callback) {
        syncApi.getSyncData(sn)
                .map(new HttpResultFunc2<SyncData>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void setSyncData(SyncDataATO syncDataATO, NetCallback<Object> callback) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create();
        String obj = gson.toJson(syncDataATO);
        RequestBody content = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),obj);

        syncApi.setSyncData(content)
                .map(new HttpResultFunc2<Object>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getSyncUserList(Long devId, NetCallback<List<User>> callback) {
        syncApi.getSyncUserList(devId)
                .map(new HttpResultFunc2<List<User>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getSyncStockList(Long devId, NetCallback<List<Stock>> callback) {
        syncApi.getSyncStockList(devId)
                .map(new HttpResultFunc2<List<Stock>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getSyncLendOrderList(Long devId, NetCallback<List<LendOrder>> callback) {
        syncApi.getSyncLendOrderList(devId)
                .map(new HttpResultFunc2<List<LendOrder>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getSyncLendOrderDetailList(Long devId, NetCallback<List<LendOrderDetail>> callback) {
        syncApi.getSyncLendOrderDetailList(devId)
                .map(new HttpResultFunc2<List<LendOrderDetail>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }
}
